package com.xydtech.web.controller.system;

import com.xydtech.common.annotation.Log;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.domain.Ztree;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.common.utils.ShiroUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.common.utils.poi.ExcelUtil;
import com.xydtech.system.domain.FivelevelAddr;
import com.xydtech.system.service.IFivelevelAddrService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 五级行政区划Controller
 *
 * @author JHON
 * @date 2021-07-11
 */
@Controller
@RequestMapping("/system/addr")
public class FivelevelAddrController extends BaseController {
    private String prefix = "system/addr";

    @Autowired
    private IFivelevelAddrService fiveLevelAddrService;

    @RequiresPermissions("system:addr:view")
    @GetMapping()
    public String addr() {
        return prefix + "/tree";
    }

    /**
     * 查询五级行政区划树列表
     */
    @RequiresPermissions("system:addr:list")
    @PostMapping("/list")
    @ResponseBody
    public List<FivelevelAddr> list(FivelevelAddr fivelevelAddr) {
        if (fivelevelAddr.getCode() == null) {
            fivelevelAddr.setCode(ShiroUtils.getSysUser().getDept().getGridCode());
        }

        List<FivelevelAddr> list = fiveLevelAddrService.selectSysFivelevelAddrList(fivelevelAddr);
        return list;
    }

    /**
     * 导出五级行政区划列表
     */
    @RequiresPermissions("system:addr:export")
    @Log(title = "五级行政区划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FivelevelAddr fivelevelAddr) {
        List<FivelevelAddr> list = fiveLevelAddrService.selectSysFivelevelAddrList(fivelevelAddr);
        ExcelUtil<FivelevelAddr> util = new ExcelUtil<FivelevelAddr>(FivelevelAddr.class);
        return util.exportExcel(list, "addr");
    }

    /**
     * 新增五级行政区划
     */
    @GetMapping(value = {"/add/{code}/{pLevel}", "/add/"})
    public String add(@PathVariable(value = "code", required = false) String code, @PathVariable(value = "pLevel", required = false) Integer pLevel, ModelMap mmap) {
        if (StringUtils.isNotNull(code)) {
            mmap.put("sysFivelevelAddr", fiveLevelAddrService.selectSysFivelevelAddrById(code));
        }
        mmap.put("pLevel", pLevel + 1);
        return prefix + "/add";
    }

    /**
     * 新增保存五级行政区划
     */
    @RequiresPermissions("system:addr:add")
    @Log(title = "五级行政区划", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FivelevelAddr fivelevelAddr) {
        return toAjax(fiveLevelAddrService.insertSysFivelevelAddr(fivelevelAddr));
    }

    /**
     * 修改五级行政区划
     */
    @GetMapping("/edit/{code}")
    public String edit(@PathVariable("code") String code, ModelMap mmap) {
        FivelevelAddr fivelevelAddr = fiveLevelAddrService.selectSysFivelevelAddrById(code);
        mmap.put("sysFivelevelAddr", fivelevelAddr);
        return prefix + "/edit";
    }

    /**
     * 修改保存五级行政区划
     */
    @RequiresPermissions("system:addr:edit")
    @Log(title = "五级行政区划", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FivelevelAddr fivelevelAddr) {
        return toAjax(fiveLevelAddrService.updateSysFivelevelAddr(fivelevelAddr));
    }

    /**
     * 删除
     */
    @RequiresPermissions("system:addr:remove")
    @Log(title = "五级行政区划", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{code}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("code") String code) {
        return toAjax(fiveLevelAddrService.deleteSysFivelevelAddrById(code));
    }

    /**
     * 选择五级行政区划树
     */
    @GetMapping(value = {"/selectAddrTree/{code}", "/selectAddrTree/"})
    public String selectAddrTree(@PathVariable(value = "code", required = false) String code, ModelMap mmap) {

        if (StringUtils.isNotNull(code)) {
            mmap.put("sysFivelevelAddr", fiveLevelAddrService.selectSysFivelevelAddrById(code));
        }
        return prefix + "/tree";
    }

    /**
     * 加载五级行政区划树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData() {

        List<Ztree> ztrees = fiveLevelAddrService.selectSysFivelevelAddrTree(ShiroUtils.getSysUser().getDept().getGridCode());
        return ztrees;
    }

    /**
     *根据父级节点查询地址列表
     * @Title: getAddrList
     * @param upCode
     * @return com.xydtech.common.core.domain.AjaxResult
     * @exception
     * @author JHON
     * @Date 2021/7/23 16:00
     **/
    @PostMapping("/getAddrList")
    @ResponseBody
    public AjaxResult getAddrList(String upCode, String code) {
        try {
            String addrList = "'<option value=\"\">所有</option>'";
            List<FivelevelAddr> list = fiveLevelAddrService.getAddrList(upCode);
            if (list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).getCode().equals(code)) {
                        addrList += "'<option value=\"" + list.get(i).getCode() + "\" selected=\"selected\">" + list.get(i).getName() + "</option>'";
                    } else {
                        addrList += "'<option value=\"" + list.get(i).getCode() + "\">" + list.get(i).getName() + "</option>'";
                    }
                }
            }
            return success(addrList);
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }
}
